/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * util.c
 *
 * Copyright (C) 2008, 2009 Lingtao Pan
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, 
 * Boston, MA 02111-1307, USA.
 */


#include "common.h"


void* perform_connection ()
{
    if (gftp_request_connect (right_viewer->request) < 0) {
        g_debug ("Connecting fails\n");
        return NULL;
    }

    /* list_files (right_viewer);   */
    gftp_viewer_get_file_list (right_viewer);
    
    return NULL;
}

void refresh_viewer (Viewer *viewer)
{
    if (viewer == left_viewer)
        refresh_left ();
    else
        refresh_right ();
}

void refresh_left ()
{
    gftp_viewer_get_file_list (left_viewer);
    gftp_viewer_show_files (left_viewer);
}


/* static void* right_view_list_dir () */
/* { */
/*   gftp_viewer_get_file_list (right_viewer); */
/*   return NULL; */
/* } */


void refresh_right ()
{
    if (right_viewer->request == NULL)
        return;

    if (right_viewer->request->running == 1) /* means a thread is running */
        return;

    gftp_delete_cache_entry (right_viewer->request, NULL, 0);

    /*  right_viewer->request->refreshing = 1; */
    enable_progressbar (gftp_interface);
    gftpui_generic_thread (gftp_viewer_get_file_list, right_viewer);
    disable_progressbar (gftp_interface);
    if ( !gftp_request_is_connected (right_viewer->request) ) {
        gftp_viewer_clear (right_viewer);
        return;
    }

    gftp_viewer_show_files (right_viewer);
}



int
make_transfer_clear_dialog ()
{
    GtkWidget *dialog, *label, *image, *hbox;
    char buf[1024];
    int result;

    dialog = gtk_dialog_new_with_buttons (_("Clear Existing Transfer List"), 
                                  GTK_WINDOW (gftp_interface->window), 
                                  GTK_DIALOG_MODAL,
                                  GTK_STOCK_OK, GTK_RESPONSE_OK,
                                  GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, 
                                  NULL);

    label = gtk_label_new (_("Clear existing transfer list?"));
    image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_QUESTION,
                                      GTK_ICON_SIZE_DIALOG);

    hbox = gtk_hbox_new (FALSE, 5);
    gtk_container_set_border_width (GTK_CONTAINER (hbox), 10);
    gtk_box_pack_start_defaults (GTK_BOX (hbox), image);
    gtk_box_pack_start_defaults (GTK_BOX (hbox), label);
    
    gtk_box_pack_start_defaults (GTK_BOX (GTK_DIALOG(dialog)->vbox), hbox);
    gtk_widget_show_all (dialog);

    result = gtk_dialog_run (GTK_DIALOG (dialog));

    gtk_widget_destroy (dialog);

    return result;
}


guint64 get_disk_free_space (const char *filename)
{
    GFile *file;
    GFileInfo *finfo;
    GError *error = NULL;
    guint64 size;

    file = g_file_new_for_path (filename);
    
    finfo = g_file_query_filesystem_info (file, 
                                  G_FILE_ATTRIBUTE_FILESYSTEM_FREE,
                                  NULL, &error);

    if (error != NULL) {
        g_debug ("query filesystem fails: %s", error->message);
        return 0;
    }

    size = g_file_info_get_attribute_uint64 (finfo, 
                            G_FILE_ATTRIBUTE_FILESYSTEM_FREE);

    g_object_unref (finfo);
    g_object_unref (file);
            
    return size;
}

void update_status_bar (Viewer *viewer)
{
    guint id;
    gchar *tempstr, *space_str;
    guint64 space;
    GtkStatusbar *bar = gftp_interface->statusbar;

    id = gtk_statusbar_get_context_id (bar, "ViewerStatus");

    if (viewer == left_viewer) {
        gtk_statusbar_pop (bar, id);
        space = get_disk_free_space (left_viewer->request->directory);
        space_str = g_format_size_for_display ((goffset) space); 
        tempstr = g_strdup_printf (_("Local Free Space: %s"), space_str);
        gtk_statusbar_push (bar, id, tempstr);

        g_free (space_str);
        g_free (tempstr);
    }
}



static GtkBuilder *
_get_all_widgets (const gchar *filename,
                  const gchar *first_required_widget,
                  va_list      args)
{
    GtkBuilder *builder;
	const char *name;
	GtkWidget **widget_ptr;
    GError *err = NULL;

	/* DEBUG ("Loading glade file %s", filename); */

    builder = gtk_builder_new ();
    gtk_builder_add_from_file (builder, filename, &err);

	if (!builder) {
		g_warning ("Couldn't find necessary glade file '%s'", filename);
	}

	for (name = first_required_widget; name; name = va_arg (args, char *)) {
		widget_ptr = va_arg (args, void *);

		*widget_ptr = (GtkWidget *) gtk_builder_get_object (builder, name);
     
		if (!*widget_ptr) {
			g_warning ("UI file '%s' is missing widget '%s'.",
                       filename, name);
			continue;
		}
	}

	return builder;
}

void
gtk_builder_get_all_widgets_simple (const gchar *filename,
                                    const gchar *first_required_widget, ...)
{
	va_list   args;
    GtkBuilder *builder;

	va_start (args, first_required_widget);

	builder = _get_all_widgets (filename, first_required_widget, args);

	va_end (args);

	if (builder)
		g_object_unref (builder);
}


GtkBuilder *
gtk_builder_get_all_widgets (const gchar *filename,
                             const gchar *first_required_widget, ...)
{
	va_list   args;
    GtkBuilder *builder;

	va_start (args, first_required_widget);

	builder = _get_all_widgets (filename, first_required_widget, args);

	va_end (args);

	if (!builder)
		return NULL;

	return builder;
}


int
gftp2_protocol_text_to_index (const char *text)
{
    int i;
    
    for (i = 0; gftp_protocols[i].name; i++) {
        if (strcmp (gftp_protocols[i].name, text) == 0)
            break;
    }

    if (gftp_protocols[i].name == NULL)
        return -1;

    return i;
}
